package chapter2.src.com.kkb.array;


import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;


/**
 * 链表  元素有序且可以重复
 */
class TestLinkedList {




    public static LinkedList<String> getStrList() {

        LinkedList<String> strings = new LinkedList<>();

        strings.add("2020-01-01");
        strings.add("2020-01-04");
        strings.add("2020-01-03");
        strings.add("2020-01-02");

        return strings;
    }



    public static LinkedList<Integer> getIntList() {

        LinkedList<Integer> integers = new LinkedList<>();
        integers.add(3);
        integers.add(1);
        integers.add(2);
        integers.add(5);
        integers.add(4);

        return integers;

    }



    public static LinkedList<Person> getPersonList() {

        LinkedList<Person> list = new LinkedList<>();
        list.add(new Person("林育容", 41));
        list.add(new Person("罗帅", 41));
        list.add(new Person("小楼", 31));
        list.add(new Person("杜光亭", 41));

        return list;

    }


    public static void testAddFirst(){
        LinkedList<Person> personList = getPersonList();
        personList.addFirst(new Person("小米", 41));
        Person first = personList.getFirst();
        System.out.println(first.toString());

    }


    public  static void testInsertIndex ( ){
        LinkedList<Person> personList = getPersonList();
        personList.add(1, new Person("小米", 41));
        personList.get(0);


    }

    public  static void testAddAll ( ){

        ArrayList<Person> list = new ArrayList<>();
        list.add(new Person("小米", 41)  );
        LinkedList<Person> personList = getPersonList();
        personList.addAll(2, list);
        System.out.println(personList.toString());


    }





    public static void main(String[] args){
        testAddAll();

    }


    @Test
    void testContains() {

        LinkedList<Person> personList = getPersonList();
        System.out.println(personList.contains(new Person("林育容", 41)));
    }


    @Test
    void testFor() {
        LinkedList<String> linkedList = new LinkedList<>();
        linkedList.add("A");
        linkedList.add("B");
        linkedList.add("C");
        linkedList.add("D");
        for (int i = 0; i < linkedList.size(); i++) {
            System.out.print(linkedList.get(i) + " ");//A B C D
        }
    }


    @Test
    void testIterator() {
        LinkedList<String> linkedList = new LinkedList<>();
        linkedList.add("A");
        linkedList.add("B");
        linkedList.add("C");
        linkedList.add("D");

        Iterator<String> listIt = linkedList.listIterator();
        while (listIt.hasNext()) {
            System.out.print(listIt.next() + " ");//A B C D
        }


        Iterator<String> it = linkedList.descendingIterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");//D C B A
        }


    }


    @Test
    void testForVsIterator() {
        LinkedList<Integer> linkedList = new LinkedList<>();
        for (int i = 0; i < 10000; i++) {
            linkedList.add(i);
        }
        long beginTimeFor = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            System.out.print(linkedList.get(i));  //为什么慢, 就是因为 每次都要调用 get(i) 方法
        }
        long endTimeFor = System.currentTimeMillis();
        System.out.println("normal for loop needed time ==> " + (endTimeFor - beginTimeFor));


        long beginTimeIte = System.currentTimeMillis();
        Iterator<Integer> it = linkedList.listIterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");   //为什么快, 就是因为直接 next;
                                                // 本来我就想直接掉调用 first 的next 方法, 但是 LinkedList 把 Node 内部类设为了私有;
        }
        long endTimeIte = System.currentTimeMillis();
        System.out.println("use iterator needed time ==> " + (endTimeIte - beginTimeIte));

    }


    @Test
    public void testSort() {

        LinkedList<Integer> intList = getIntList();
       // intList.sort((var1, var2) -> var1.compareTo(var2));
        System.out.println(intList.toString());
    }


    @Test
    public void testSort1() {


    //   LinkedList<String> latestMonth = getLatestMonth("2020-08-04", 6);
    //   System.out.println(latestMonth.toString());
    }

   /* public static LinkedList<String> getLatestMonth(String date, int num) {
        LocalDate today = LocalDate.parse(date);
        LinkedList<String> monthList = new LinkedList<>();

        for (int i = 1; i <= num; i++) {
            LocalDate localDate = today.minusMonths(i);
            String month = localDate.toString().substring(0, 7);
            monthList.add(month);
        }

        monthList.sort((var1, var2) -> {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date1 = simpleDateFormat.parse(var1 + "-01");
                Date date2 = simpleDateFormat.parse(var2 + "-01");
                return date1.compareTo(date2);
            } catch (ParseException e) {
                e.printStackTrace();
                return -1;
            }
        });

        return monthList;
    }
*/

}
/*
*   www.pearsonhighered.com/cssupport
*
* */